/**
 * Definition for singly-linked list.
 * */
function ListNode(val, next) {
    this.val = (val === undefined ? 0 : val)
    this.next = (next === undefined ? null : next)
}

/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */
var removeNthFromEnd = function (head, n) {
    let nodeArr = []
    let currentNode = head

    while (currentNode) {
        nodeArr.push(currentNode)
        currentNode = currentNode.next
    }

    let len = nodeArr.length
    currentNode = head
    if (len === n) {
        return currentNode.next
    }
    let i = nodeArr.length - n - 1
    nodeArr[i].next = nodeArr[i].next.next
    return head
};
let inputNode = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, null))))

removeNthFromEnd(inputNode, 1)
//================v2要点总结===============
/**
 * 1. 本版本用额外的空间，来存储节点，遍历完成之后，开始删除
 */